package com.leo.m1803.d11;

/**
 * Created by LEO on 2018/3/11.
 */
public class Offer08 {
    public static int findMin(int[] a){
        int i = 0, j = a.length-1;
        while (i < j){
            if (i == j-1){ break; }
            int mid = (i+j)/2;
            if (a[mid] == a[i] && a[mid] == a[j]){
                int temp = a[i];
                for (int k = i+1; k <= j ; k++) {
                    if (temp < a[k] ){
                        temp = a[k];
                    }else if (temp > a[k]){
                        temp = a[k];
                        break;
                    }
                }
                return temp;
            }

            if (a[mid] > a[i]){
                i = mid;
            }else if (a[mid] < a[j]){
                j = mid;
            }

        }
        return a[j];
    }

    public static void main(String[] args) {
        System.out.println(findMin(new int[]{2,3,4,5,1}));
        System.out.println(findMin(new int[]{3,4,5,1,2}));
        System.out.println(findMin(new int[]{2,3,4,1,2,2}));
        System.out.println(findMin(new int[]{2,2,2,2,2,2}));
    }
}
